import { useEffect } from "react";
import { useLocation, useNavigate } from "react-router-dom";
import { IUser } from "renderer/contexts/GlobalContext";
import useGlobal from "renderer/hooks/useGlobal";
import { getUserDetail } from "renderer/services/api";


export default function Login() {

	// console.log('login render')
	const navigate = useNavigate()
	const location = useLocation()

	const { setUserInfo } = useGlobal()

	const from = (location.state as any).from.pathname || "/";

	useEffect(() => {

		window.electron.ipcRenderer.login()

		window.electron.ipcRenderer.on('user-info', async (arg: IUser) => {
			// console.log("arg =", arg);
			setUserInfo(arg)

			// 跳转到首页
			navigate(from, {replace: true})

			if(arg.token !== '') {
				getUserDetail(arg.infoToken)
					.then(resp => {
						console.log('resp 2==', resp)
						// 设置用户信息
						setUserInfo({
							...arg, 
							name: resp.data.params.realname,
							account: resp.data.params.realname,
							id: resp.data.params.id,
							tenant_id: resp.data.params.tenant_id
						})
					})
					.catch(error => {
						// 获取用户信息失败
						window.electron.log.info('get user infosimple err: ', error)
					})
      	
			}
		})
	}, [])

	

	return (
		<>
		</>
	)
}